<template>
  <div>
    <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="100px">
      <el-form-item label="客户姓名" prop="cName">
        <el-input v-model="ruleForm.cName" placeholder="请输入客户姓名" style="width: 260px"></el-input>
      </el-form-item>
      <el-form-item label="身份证号" prop="idcardNum">
        <el-input v-model="ruleForm.idcardNum" placeholder="请输入身份证信息" style="width: 260px"></el-input>
      </el-form-item>
      <el-form-item label="送机时间" prop="postTime">
        <el-date-picker
          style="width: 260px"
          v-model="ruleForm.postTime"
          type="datetime"
          placeholder="选择日期时间"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="客户类型" prop="cType">
        <el-select v-model="ruleForm.cType" placeholder="请选择" style="width: 260px">
          <el-option label="家庭用户" value="1"></el-option>
          <el-option label="单位用户" value="2"></el-option>
          <el-option label="代理商" value="3"></el-option>
          <el-option label="签约用户" value="4"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="座机" prop="cCall">
        <el-input v-model="ruleForm.cCall" placeholder="请输入座机号码" style="width: 260px"></el-input>
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input v-model="ruleForm.phone" placeholder="请输入手机号码" style="width: 260px"></el-input>
      </el-form-item>
      <el-form-item label="邮编" prop="postNum">
        <el-input v-model="ruleForm.postNum" placeholder="请输入邮政编码" style="width: 260px"></el-input>
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="ruleForm.email" placeholder="请输入邮箱账号" style="width: 260px"></el-input>
      </el-form-item>
        <!--<el-col span='8'>
          <el-form-item label="故障类型" prop="faultType">
            <el-select style="width: 260px" v-model="form.faultType" placeholder="请选择">
              <el-option label="固定性故障" value="1"></el-option>
              <el-option label="间歇性故障" value="2"></el-option>
            </el-select>
          </el-form-item>
        </el-col>-->
      <el-form-item label="单位名称" prop="unitName">
        <el-input v-model="ruleForm.unitName" placeholder="请输入客户单位名称" style="width: 260px"></el-input>
      </el-form-item>
      <el-form-item label="地址" prop="cAddress">
        <el-input type="textarea" v-model="ruleForm.cAddress" placeholder="请输入客户地址" style="width: 260px"></el-input>
      </el-form-item>
     <el-form-item style="margin-top: 20px">
        <el-button type="primary" @click="submitForm">保存修改</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
    
  </div>
</template>
<script>
import {submitForm as submitForm} from '../../api/client';
import {getUserInfo as getUserInfo} from '../../api/client';
//import { getMap } from "../../utils/dataStorage";

export default {
  props:["cId"],
  data() {
    return {
      ruleForm: {
        cId: "",
        cName: "",
        idcardNum: "",
        postTime: "",
        cType: "",
        cCall: "",
        phone: "",
        postNum: "",
        email: "",
        unitName: "",
        cAddress: "",
        createTime: "",
        active:"1"
        //createTime: ""
      },
      rules: {
        cName: [
          { required: true, message: "请输入客户姓名", trigger: "blur" },
          { min: 1, max: 10, message: "长度在 1-10个字符", trigger: "blur" }
        ],
        idcardNum: [
          { required: true, message: "请输入客户身份证号", trigger: "blur" }
        ],
        postTime: [
          {
            type: "date",
            required: true,
            message: "请选择客户送机日期",
            trigger: "change"
          }
        ],
        cType: [
          { required: true, message: "请选择客户类型", trigger: "blur" }
        ],
        
        postNum: [
          { required: true, message: "请输入邮编", trigger: "blur" }
        ],
        email: [
          { required: true, message: "请输入电子邮箱", trigger: "blur" }
        ],
        
        cAddress: [
          { required: true, message: "请输入单位地址", trigger: "blur" }
        ],
      },
       buttonText:"修改"
      //buttonText:"创建"
    }
  },
  created(){
        if(this.cId){
                let params = new URLSearchParams();
                params.append('cId', this.cId);
                this.getUserInfo(params);
            }
           
    },
  components: {},
  methods: {
    submitForm() {
          let param = new URLSearchParams();
          param.append('cId',this.cId);
          param.append("cName", this.ruleForm.cName);
          param.append("idcardNum", this.ruleForm.idcardNum);
          let time = this.ruleForm.postTime;
          let year = time.getFullYear();
          let month = time.getMonth()+1;
          let day = time.getDate();
          time = year + "-" + month + "-" + day;
          param.append("postTime",time);
          //console.log(this.dateFormat(time));
          param.append("cType", this.ruleForm.cType);
          param.append("cCall",this.ruleForm.cCall);
          param.append("phone",this.ruleForm.phone);
          param.append("postNum",this.ruleForm.postNum);
          param.append("email",this.ruleForm.email);
          param.append("unitName",this.ruleForm.unitName);
          param.append("cAddress",this.ruleForm.cAddress);
          param.append("active",this.ruleForm.active);
          if(this.ruleForm.cName == ""){
            alert("请输入客户姓名！");
          }else if(this.ruleForm.idcardNum == ""){
            alert("请输入客户身份证号！");
          }
          
            // let time = this.ruleForm.postTime;
            // let year = time.getFullYear();
            // let month = time.getMonth()+1;
            // let day = time.getDate();
            // time = year + "-" + month + "-" + day;
            // param.append("postTime",time);
          else if(this.ruleForm.cType == ""){
            alert("请选择客户类型！");
          }else if(this.ruleForm.postNum == ""){
            alert("请输入邮编！");
          }else if(this.ruleForm.email == ""){
            alert("请输入邮箱账号！");
          }else if(this.ruleForm.cAddress == ""){
            alert("请输入客户地址！");
          }else{
              submitForm(param).then(r=>{
                setTimeout(() => {
                  this.$notify({
                    
                    title:"修改客户成功",
                    type:"success"
                  });
                  //this.$router.push({ path: "/dclient/update"});
                },1000);
              
              }).catch(_=>{
                  console.log("修改客户信息失败！");
              });
         }
    },
   
    resetForm(formName){
        this.$refs[formName].resetFields();
    },
    getUserInfo(params) {
      
      let that = this;
                // 执行方法
                getUserInfo(params).then(r=>{
                  console.log("进入");
                    console.log(r);
                   // that.ruleForm = r;
                    this.ruleForm.cName = r.cName
                    this.ruleForm.idcardNum=r.idcardNum;
                    this.ruleForm.postTime = new Date(r.postTime);
                    this.ruleForm.cType = r.cType + "";
                    this.ruleForm.cCall = r.cCall;
                    this.ruleForm.phone = r.phone;
                    this.ruleForm.postNum = r.postNum;
                    this.ruleForm.email = r.email;
                    this.ruleForm.unitName = r.unitName;
                    this.ruleForm.cAddress = r.cAddress;
                    this.ruleForm.active = r.active;

                }).catch(_=>{
                    console.log("获取客户信息失败！");
                })
    }
  }
};
</script>